<?php

namespace app\admin\controller;

use app\models\{Business, PlatClass, Recpeople, PlatSons, Goods, GoodsSpec, Region, BusinessArea, Compent, User as Users, Order as Orders, OrderGoods, Payment, Coupon, Wechat};
use think\Db;
use think\Validate;

class Order extends Base
{

    //正常订单
    public function order()
    {
        $results = (new Orders())->where('shouhou', 1)->order(['id' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['businessid'])) {
            $results = $results->where('businessid', $data['businessid']);
        }
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn|id|code', $data['ordersn']);
        }
        if (!empty($data['status'])) {
            $results = $results->where('status', $data['status']);
        }
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
        }
        $shop = (new Business())->field('id,title,account')->where('status', 1)->select();
        return view(null, [
            'list' => $results,
            'shop' => $shop
        ]);
    }

    //售后订单
    public function order1()
    {
        $results = (new Orders())->where('shouhou', 2)->order(['id' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['businessid'])) {
            $results = $results->where('businessid', $data['businessid']);
        }
        if (!empty($data['ordersn'])) {
            $results = $results->where('ordersn|id|code', $data['ordersn']);
        }
        if (!empty($data['status'])) {
            $results = $results->where('status', $data['status']);
        }
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['business'] = (new Business())->where(['id' => $result['businessid']])->value('title');
            $result['status_name'] = $result->status_text;
            $result['user'] = (new Users())->field('nickName,avatarUrl')->where('id', $result['user_id'])->find();
            $goods = (new OrderGoods())->where(['ordersn' => $result['ordersn']])->select();
            foreach ($goods as $good) {
                $good['images'] = json_decode($good['images'], true);
            }
            $result['good'] = $goods;
        }
        $shop = (new Business())->field('id,title,account')->where('status', 1)->select();
        return view(null, [
            'list' => $results,
            'shop' => $shop
        ]);
    }

    public function order_str()
    {
        $data = $this->request->post();
        if (empty($data['id']) || empty($data['status'])) {
            return  json(['status' => 1, 'code' => 1, 'msg' => '信息参数错误!']);
        }
        $order = (new Orders())->where('id', $data['id'])->find();
        if (empty($order)) {
            return  json(['status' => 1, 'code' => 1, 'msg' => '订单不存在!']);
        }
        if ($order['status'] == 4 || $order['status'] == 5) {
            return json(['status' => 1, 'code' => 1, 'msg' => '订单已完成或取消，无法继续操作!']);
        }
        switch ($data['status']) {
            case '2':
                break;
            case '3':
                if ($order['status'] != 2) {
                    return json(['status' => 1, 'code' => 1, 'msg' => '订单状态不可配送!']);
                }
                $res = (new Orders())->where('id', $data['id'])->update(['status' => 3, 'fwtimes' => date('Y-m-d H:i:s')]);
                if ($res) {
                    return  json(['status' => 0, 'code' => 0, 'msg' => '订单已配送中!']);
                }
                break;
                //完成订单
            case '4':
                if ($order['status'] != 2 && $order['status'] != 3) {
                    return json(['status' => 1, 'code' => 1, 'msg' => '订单状态不可完成!']);
                }
                $res = (new Orders())->where('id', $data['id'])->update(['status' => 4, 'tuikuan' => 0, 'finishtimes' => date('Y-m-d H:i:s')]);
                if ($res) {
                    return  json(['status' => 0, 'code' => 0, 'msg' => '订单已完成!']);
                }
                break;
                //取消订单
            case '5':
                //未支付订单直接取消
                if ($order['status'] == 1) {
                    Db::startTrans();
                    try {
                        $res = (new Orders())->where('id', $data['id'])->update(['status' => 5, 'caceltimes' => date('Y-m-d H:i:s')]);
                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                        return json(['status' => 1, 'code' => 1, 'msg' => '操作失败，请重试!']);
                    }
                    if ($res) {
                        return json(['status' => 0, 'code' => 0, 'msg' => '订单已取消!']);
                    }
                    return json(['status' => 1, 'code' => 1, 'msg' => '操作失败，请重试!']);
                }
                //已支付订单递交平台处理退款等操作
                if ($order['status'] == 2 || $order['status'] == 3) {
                    $res = (new Orders())->where('id', $data['id'])->update(['tuikuan' => 2, 'text' => '平台取消订单']);
                    if ($res) {
                        return  json(['status' => 0, 'code' => 0, 'msg' => '订单已提交平台退款处理!']);
                    }
                }
                //商家拒绝退款
            case '99':
                $res = (new Orders())->where('id', $data['id'])->update(['shouhou' => 1, 'tuikuan' => 3]);
                if ($res) {
                    return  json(['status' => 0, 'code' => 0, 'msg' => '订单已拒绝!']);
                }
                break;
            default:
                return json(['status' => 1, 'code' => 1, 'msg' => '操作状态出错!']);
                break;
        }
    }

    public function orderdetail()
    {
        $ordersn = $this->request->get('ordersn/d', 0);
        $list = (new OrderGoods())->where(['ordersn' => $ordersn])->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $lists['images'] = json_decode($lists['images'], true);
        }
        return view(null, [
            'list' => $list
        ]);
    }
}